function ListNode(val, next) {
  this.val = val == undefined ? 0 : val;
  this.next = next == undefined ? null : next;
}

function createList(values){
    let nodes = values.map(item => {
        return new ListNode(item)
    })
    for(let i = 0; i < nodes.length; i++){
        nodes[i].next = nodes[i + 1]
    }
    return nodes[0]
}

function printList(head){
    let current = head;
    while(current){
        console.log(current.val);
        current = current.next
    }
}

function mergeTwoLists(list1, list2) {
    // console.log('list1:', list1);
    // console.log('list2:', list2);
    let dummy = new ListNode(0)
    let current = dummy
    while(list1 && list2){
        if(list1.val < list2.val){
            current.next = list1
            list1 = list1.next
        }else{
            current.next = list2
            list2 = list2.next
        }
        current = current.next
    }
    // if(list1){
    //     current.next = list1
    // }
    // if(list2){
    //     current.next = list2
    // }

    // console.log('dummy:', dummy);
    return dummy.next
    
}


let l1 = createList([1,2,4]);
let l2 = createList([1,3,4]);


let root = mergeTwoLists(l1, l2)
printList(root)